 // gjw/pages/doubao/my.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    menu:['全部','待审核','未通过','已通过'],
    activeIndex:0,
    index:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		var siteUrl = wx.getStorageSync('siteUrl')
		// app.util.request({
		// 	url: 'entry/wxapp/Recostis',
		// 	data: { openid: wx.getStorageSync('openid') },
		// 	success: function (res) {
		// 		if (res.data.code == 200) {
		// 			console.log(res)
		// 			that.setData({
		// 				total: res.data.total
		// 			})
		// 		}
		// 	}
		// })
		app.util.request({
			url: 'entry/wxapp/Repucostdbut',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						restups: res.data.restups,
						resttops: res.data.resttops
					})
				}
			}
		})
    that.setData({
      siteUrl: siteUrl
    })
    that.money()
    that.getAll()
    that.one()
    that.two()
    that.three()
  },

  tabClick: function (e) {
    
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
    })
  },
  waite:function(e){
    wx.showModal({
      title: '提示',
      content: '您的抖宝正在审核中请耐心等待哦~',
      showCancel:false,
      success:function(res){
        if (res.confirm) {
          
        }
      }
    })
  },
  pass:function(e){
    wx.showModal({
      title: '提示',
      content: '您的抖宝审核未通过，发布费会在1~3个工作日退回，请注意查看',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          
        }
      }
    })
  },
  
  
  // 获取全部
  getAll:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url:'entry/wxapp/Dball',
      data:{openid:openid},
			success: function (res) {
        
       if(res.data.code==200){
         that.setData({
           dblist: res.data.res
         })
       }else if(res.data.code==500){
         that.setData({
           dblist:[]
         })
       }
      },
    })
  },

  // 待审核
  one: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Dbnsh',
      data: { openid: openid },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            dbone: res.data.res
          })
        } else if (res.data.code == 500) {
          that.setData({
            dbone: []
          })
        }
      },
    })
  },


  // 未通过
  two: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Dbjsh',
      data: { openid: openid },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            dbtwo: res.data.res
          })
        } else if (res.data.code == 500) {
          that.setData({
            dbtwo: []
          })
        }
      },
    })
  },

  // 已通过
  three: function (e) {
    var that = this
		var openid = wx.getStorageSync('openid')
    app.util.request({
      url: 'entry/wxapp/Dbysh',
      data: { openid: openid },
      success: function (res) {
        
        if (res.data.code == 200) {
          that.setData({
            dbthree: res.data.res
          })
        } else if (res.data.code == 500) {
          that.setData({
            dbthree: []
          })
        }
      },
    })
  },

  delete:function(e){
    var that = this
    
    var id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确定删除该抖宝？',
      success: function (res) {
        if (res.confirm) {
          
          app.util.request({
            url: 'entry/wxapp/Dbdelete',
            data: { id: id },
            success: function (res) {
              
              if (res.data.code == 200) {
                wx.showToast({
                  title: '删除成功',
                })
                that.getAll()
                that.three()
              }
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })
  },

  // 设置抖宝发布费用
  money: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Dbmoney',
      success: function (res) {
        that.setData({
          money: res.data.res.upfee,
          cash: res.data.res.topfee,
        })
      }
    })
  },

  tuijian:function(e){
    var that=this
    var res_id=e.currentTarget.dataset.id
    var openid = wx.getStorageSync('openid')  
    var money = Number(that.data.money ) 
    wx.showModal({
      title: '提示',
      content: '是否确认推荐本条抖宝？',
      success: function (res) {
        if (res.confirm) {
					if(that.data.money>0){
						if (that.data.restups>0){
							app.util.request({
								url: 'entry/wxapp/dbupsuccess',
								data: {
									id: res_id, openid:openid,tuijian:1
								},
								success: function (res) {
									
									if (res.data.code == 200) {
										wx.showModal({
											title: '提示',
											content: '已成功推荐本抖宝，去首页查看',
											success: function (res) {
												if (res.confirm) {
													wx.reLaunch({
														url: '../index/index',
													})
												} else if (res.cancel) {
													that.getAll()
													that.three()
												}
											}
										})
									} else {
										wx.showModal({
											title: '提示',
											content: '推荐失败，请重试！',
											success: function (res) {
												if (res.confirm) {

												} else if (res.cancel) {

												}
											}
										})
									}
								}
							})
						}else{
							app.util.request({
								url: 'entry/wxapp/Dbuppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/dbupsuccess',
												data: {
													id: res_id
												},
												success: function (res) {
													wx.showModal({
														title: '提示',
														content: '已成功推荐本抖宝，去首页查看',
														success: function (res) {
															if (res.confirm) {
																wx.reLaunch({
																	url: '../index/index',
																})
															} else if (res.cancel) {
																that.getAll()
																that.three()
															}
														}
													})
												}
											})

										},
										'fail': function (res) {
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					}else{
						app.util.request({
							url: 'entry/wxapp/dbupsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								if (res.data.code == 200) {
									wx.showModal({
										title: '提示',
										content: '已成功推荐本抖宝，去首页查看',
										success: function (res) {
											if (res.confirm) {
												wx.reLaunch({
													url: '../index/index',
												})
											} else if (res.cancel) {
												that.getAll()
												that.three()
											}
										}
									})
								} else {
									wx.showModal({
										title: '提示',
										content: '推荐失败，请重试！',
										success: function (res) {
											if (res.confirm) {

											} else if (res.cancel) {

											}
										}
									})
								}
							}
						})
					}

        } else if (res.cancel) {
          
        }
      }
    })
  },
  tops: function (e) {
    var that = this  
    var res_id = e.currentTarget.dataset.id
    var cash=Number(that.data.cash)
    var openid = wx.getStorageSync('openid')
    wx.showModal({
      title: '提示',
      content: '是否确认置顶本条抖宝？',
      success: function (res) {
        if (res.confirm) { 
					if (that.data.cash > 0) {
						if (that.data.resttops > 0) {
							app.util.request({
								url: 'entry/wxapp/Dbtopsuccess',
								data: {
									id: res_id, openid: openid, tops: 1
								},
								success: function (res) {
									if (res.data.code == 200) {
										wx.showModal({
											title: '提示',
											content: '已成功置顶本抖宝，去首页查看',
											success: function (res) {
												if (res.confirm) {
													wx.reLaunch({
														url: '../index/index',
													})
												} else if (res.cancel) {
													that.getAll()
													that.three()
												}
											}
										})
									} else {
										wx.showModal({
											title: '提示',
											content: '置顶失败，请重试！',
											success: function (res) {
												if (res.confirm) {

												} else if (res.cancel) {

												}
											}
										})
									}
								}
							})
						} else {
							app.util.request({
								url: 'entry/wxapp/Dbtoppay',
								data: {
									openid: openid,
								},
								success: function (res) {
									wx.requestPayment({
										'timeStamp': res.data.timeStamp,
										'nonceStr': res.data.nonceStr,
										'package': res.data.package,
										'signType': 'MD5',
										'paySign': res.data.paySign,
										'success': function (res) {
											app.util.request({
												url: 'entry/wxapp/Dbtopsuccess',
												data: {
													id: res_id
												},
												success: function (res) {
													wx.showModal({
														title: '提示',
														content: '已成功置顶本抖宝，去首页查看',
														success: function (res) {
															if (res.confirm) {
																wx.reLaunch({
																	url: '../index/index',
																})
															} else if (res.cancel) {
																that.getAll()
																that.three()
															}
														}
													})
												}
											})

										},
										'fail': function (res) {
											wx.showToast({
												title: '支付失败',
												icon: "loading",
											})
										}
									})
								}
							})
						}
					} else {
						app.util.request({
							url: 'entry/wxapp/Dbtopsuccess',
							data: {
								id: res_id
							},
							success: function (res) {
								if (res.data.code == 200) {
									wx.showModal({
										title: '提示',
										content: '已成功置顶本抖宝，去首页查看',
										success: function (res) {
											if (res.confirm) {
												wx.reLaunch({
													url: '../index/index',
												})
											} else if (res.cancel) {
												that.getAll()
												that.three()
											}
										}
									})
								} else {
									wx.showModal({
										title: '提示',
										content: '置顶失败，请重试！',
										success: function (res) {
											if (res.confirm) {

											} else if (res.cancel) {

											}
										}
									})
								}
							}
						})
					}
        } else if (res.cancel) {
          
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this
    var activeIndex=that.data.activeIndex
    if(activeIndex==0){
      that.getAll()
    }else if(activeIndex==1){
      that.one()      
    } else if (activeIndex == 2) {
      that.two()      
    } else if (activeIndex == 3) {
      that.three()      
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this
    var openid = wx.getStorageSync('openid')    
    var activeIndex = that.data.activeIndex
    that.data.page=that.data.page+1
    if (activeIndex==0){
      app.util.request({
        url: 'entry/wxapp/Dball',
        data: { openid: openid,page:that.data.page},
        success: function (res) {
          if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.dblist.push(res.data.res[i])
            }
            that.setData({
              dblist: that.data.dblist
            })
          } else if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          }
        },
      })
    } else if (activeIndex==1){
      app.util.request({
        url: 'entry/wxapp/Dbnsh',
        data: { openid: openid,page:that.data.page },
        success: function (res) {
          
          if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.dbone.push(res.data.res[i])
            }
            that.setData({
              dbone: that.data.dbone
            })
          } else if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          }
        },
      })
    }else if(activeIndex==2){
      app.util.request({
        url: 'entry/wxapp/Dbjsh',
        data: { openid: openid,page:that.data.page },
        success: function (res) {
          
          if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.dbtwo.push(res.data.res[i])
            }
            that.setData({
              dbtwo: that.data.dbtwo
            })
          } else if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          }
        },
      })
    }else if(activeIndex==3){
      app.util.request({
        url: 'entry/wxapp/Dbysh',
        data: { openid: openid, page: that.data.page },
        success: function (res) {
          
          if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.dbthree.push(res.data.res[i])
            }
            that.setData({
              dbthree: that.data.dbthree
            })
          } else if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          }
        },
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})